package cfca.paperless.client.servlet._9_unite;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;
import cfca.paperless.client.util.StringUtil;
import cfca.sadk.util.Base64;


/**
 * @Description 联合接口( PDF合成外部签名并签章接口,PDF转图片)
 * @author zf
 * @CodeReviewer ts
 */
public class PaperlessUniteTest06 {
   
    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();// 开始时间
            // 初始化客户端对象
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.extUrl, 10000, 60000);// 无纸化扩展接口的访问URL
            PaperlessClient clientBean4Assist = new PaperlessClient(PaperlessConfig.assistUrl, 10000, 60000);// 无纸化辅助接口的访问URL
            
            
            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;
           
            String sigString = "MIIE8AYJKoZIhvcNAQcCoIIE4TCCBN0CAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHAaCCA7EwggOtMIIClaADAgECAgUQAnZCAzANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJDTjEwMC4GA1UEChMnQ2hpbmEgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRcwFQYDVQQDEw5DRkNBIFRFU1QgT0NBMTAeFw0xNjA0MTEwNDMyMTJaFw0xOTA1MTEwNDMyMTJaMHAxCzAJBgNVBAYTAkNOMRcwFQYDVQQKEw5DRkNBIFRFU1QgT0NBMTERMA8GA1UECxMITG9jYWwgUkExFTATBgNVBAsTDEluZGl2aWR1YWwtMTEeMBwGA1UEAxQV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AgUQAnZCAzAJBgUrDgMCGgUAMA0GCSqGSIb3DQEBAQUABIGAg1PNHgJ4GVxh511jw0A4MrMtgG6Vr7ik76Yd1O9Y3ZYy3zPvaQOvxChNf6kxxB0Q2k19O2mhOHqWJxjVWl+61PeERKk8SYm82bU2f/2ZAkU9FjnQlr9KRUQgFbYEn7ZsWFPRRM33UWmozecEdqC/WzbPEW54FD/IRCGvJsFzfW8=";
            byte[] signature = Base64.decode(sigString);
            
            String id = "201712011437174835223212612677";
            
            String bizSerialNo = GUID.generateId();
            
            String bizTypeCode = "B001";
            String outputFilePath = "auto";
            
            PdfBean pdfBean = new PdfBean("", bizSerialNo, bizTypeCode, outputFilePath, "");
            pdfBean.setPdfId(id);
            String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
            String sealStrategyListXml = "";//createSealStrategyListXml();
            // 合成签名并签章
            byte[] sealedPdfData = clientBean.synthesizeOuterSignatureAndSealPdf(signature, pdfBeanXml, sealStrategyListXml, operatorCode, channelCode);
            String errorRsString = PaperlessUtil.isError(sealedPdfData);

            if ("".equals(errorRsString)) {
                System.out.println("OK:synthesizeOuterSignatureAndSealPdf");
              
                IoUtil.write("./TestData/output/"+bizSerialNo+".pdf", sealedPdfData);
                
                // ////////////////////////////////PDF自动化转图片(transformPdfToImage)///////////////////////////////////
                String pageNo = "-1";
                byte[] pdfFile = sealedPdfData;
                
                String systemNo = GUID.generateId();//业务流水号 
                
                String result = clientBean4Assist.transformPdfToImage(systemNo,pdfFile, pageNo, PaperlessConfig.operatorCode, PaperlessConfig.channelCode);
                String code = StringUtil.getNodeText(result, "Code");
                if (!code.equals("200")) {
                    System.out.println(result);
                } else {
                    int size = 1;
                    String imageBase64 = StringUtil.getNodeText(result, "Image");
                    if (pageNo.equals("-1") || pageNo.equals("0") ) {
                        String[] imageData = imageBase64.split(":");
                        System.out.println("imageData size:" + imageData.length);
                        size = imageData.length;
                        for (int i = 0; i < imageData.length; i++) {
                            IoUtil.write("./TestData/output/" + bizSerialNo +"-"+ (i + 1) + ".png", Base64.decode(imageData[i]));
                        }
                    } else {
                        IoUtil.write("./TestData/output/"+bizSerialNo+"-1.png", Base64.decode(imageBase64));
                       
                    }
                   
                    ArrayList<String> pdfList = new ArrayList<String>();
                    File dir = new File("./TestData/output/"+bizSerialNo+"-1.png");
                    String absolutePath = dir.getAbsolutePath();
                    
                    pdfList.add(absolutePath);//绝对路径
                    pdfList.add(bizSerialNo+".pdf");
                    pdfList.add(bizSerialNo+"-1.png");
                    pdfList.add(size+"");
                    
                    System.out.println("OK:transformPdfToImage");
                }
            } else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("NG:synthesizeOuterSignatureAndSealPdf");
                System.out.println(sealedPdfData.length);
                System.out.println(new String(sealedPdfData, "UTF-8"));
            }
            long timeEnd = System.currentTimeMillis();// 结束时间
            System.out.println("time:" + (timeEnd - timeStart));
        } catch (Exception e) {
            e.printStackTrace();
        }
        
      
    }
    
    
    
    private static String createSealStrategyListXml() throws Exception {
        String sealCode = "haier_rzzl";
        String sealPassword = "";
        
        SealUserInfo sealUserInfo = new SealUserInfo("张三", "beijing", "reason");//sealUserInfo.setVisible(false);
        // 3=关键字签章
        SignLocation signLocation = new SignLocation(1, 400, 400);
        
        // 生成签章策略文件
        String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocation);
        
        return "<List>" + sealStrategyXml + "</List>";
    }
    
    
}
